package com.rc.saas.tenant.controller.ticket;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ticket.TenantTicketTypeExample;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.service.tenant.TenantUserService;
import com.rc.saas.tenant.service.ticket.TenantTicketTypeService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 优惠券管理
 */
@Controller
@RequestMapping("/tenant/ticket")
public class TicketController extends TenantBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private String prefix = "/templates/tenant/ticket";
    private final String sessionModifyPwdPrefix = "modifyPwd";

    @Autowired
    private TenantUserService tenantUserService;

    @Autowired
    private TenantService tenantService;
    @Autowired
    private TenantTicketTypeService tenantTicketTypeService;


    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return prefix + "/user";
    }

    @RequestMapping(value = "/onlineUser", method = RequestMethod.GET)
    public String onlineUser(Model model) {
        return prefix + "/onlineUser";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo list(@RequestParam Map<String, String> params) {
        //CmsUserExample example = new CmsUserExample();

        //if (StringUtils.isNotBlank(params.get("loginName"))) {
        //    example.createCriteria().andLoginNameLike(params.get("loginName"));
        //}

        //PageInfo<CmsUser> page = tenantUserService.selectUserByPage(example, Integer.valueOf(params.get("offset")), Integer.valueOf(params.get("limit")));

        TenantUser record = new TenantUser();
        PageInfo<TenantUser> page = tenantUserService.selectUserByPage(record, Integer.valueOf(params.get("offset")), Integer.valueOf(params.get("limit")));

        WebPageInfo webPageInfo = new WebPageInfo(page.getList(), Long.valueOf(page.getTotal()).intValue());

        return webPageInfo;
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public Object listAll() {
        TenantUser record = ShiroUtils.getSessionUser();
        TenantTicketTypeExample example=new TenantTicketTypeExample();
        example.createCriteria().andTenantCodeEqualTo(record.getTenantCode()).andStatusEqualTo("0").andIsMaxEqualTo("1");
        example.setOrderByClause(" created_time desc ");
        return TResponse.SUCCESS_DATA(tenantTicketTypeService.selectByExample(example));
    }

}
